//index.js
//获取应用实例
import {deviceApi} from "../../model/aliyun/device";
import {storage} from "../../utils/storage";
const app = getApp();

Page({
  _data: {
      PageSize: 50,
      CurrentPage: 1,
  },

  data: {
      deviceOnLine: [],
      deviceOffLine: [],
      deviceUnative: [],
      deviceDisable: [],
      showConfig: false,
  },
  onLoad: function () {
  },

  onShow() {
      if (!app.aliConfig.RegionId || !app.aliConfig.AccessKeyId
          || !app.aliConfig.ProductKey
          || !app.aliConfig.EndPoint){
          this.setData({
              showConfig: true
          });
      } else {
          this.setData({
              showConfig: false
          });
          wx.showLoading({
              title: '努力加载中...',
              mask: true
          });
          this.loadDeviceList();
      }
  },

  onPullDownRefresh() {
      this.loadDeviceList();
  },

  loadDeviceList() {
      // 先清空数据
      this.setData({
          deviceOnLine: [],
          deviceOffLine: [],
          deviceUnative: [],
          deviceDisable: [],
      }, () => {
          this._data.CurrentPage = 1; //从第一页开始
          this.queryDevice(this._data.PageSize, this._data.CurrentPage);
      });
  },

  // 查询设备
  queryDevice(pageSize = 50, currentPage = 1) {
      deviceApi.queryDevice({
          PageSize: pageSize,
          CurrentPage: currentPage,
      }).then((res) => {
          if (res.Data && res.Data.DeviceInfo) {
              this.handleDeviceList(res.Data.DeviceInfo);
              // 判断是否需要继续请求
              if (res.PageCount > this._data.CurrentPage) {
                  this._data.CurrentPage ++;
                  this.queryDevice(this._data.PageSize, this._data.CurrentPage);
              } else {
                  this.finishQueryDevice();
              }
          } else {
              this.finishQueryDevice();
          }
      }).catch(err => {
          console.log(err);
          this.finishQueryDevice();
      });
  },

  finishQueryDevice(){
      wx.stopPullDownRefresh();
      wx.hideLoading && wx.hideLoading();
  },

  // 处理列表返回内容
  handleDeviceList(deviceList) {
      let deviceOnLine = [];
      let deviceOffLine = [];
      let deviceUnative = [];
      let deviceDisable = [];

      if(deviceList && Array.isArray(deviceList)) {
           deviceList.forEach((device) => {
              let type;
              if (device.Nickname.indexOf('_led2') > -1) {
                  type = '_led2';
              } else if (device.Nickname.indexOf('_led3') > -1) {
                  type = '_led3';
              } else if (device.Nickname.indexOf('_lamp1') > -1) {
                  type = '_lamp1';
              } else if (device.Nickname.indexOf('_lamp2') > -1) {
                  type = '_lamp2';
              } else {
                  type = '_led1';
              }
              this.setDevice(device,type);

              if (device.DeviceStatus === 'ONLINE') {
                  deviceOnLine.push(device);
              } else if (device.DeviceStatus === 'OFFLINE') {
                  deviceOffLine.push(device);
              } else if (device.DeviceStatus === 'UNACTIVE') {
                  deviceUnative.push(device);
              } else if (device.DeviceStatus === 'DISABLE') {
                  deviceDisable.push(device);
              }
           });
      }
      this.setData({
          deviceOnLine: this.data.deviceOnLine.concat(deviceOnLine),
          deviceOffLine: this.data.deviceOffLine.concat(deviceOffLine),
          deviceUnative: this.data.deviceUnative.concat(deviceUnative),
          deviceDisable: this.data.deviceDisable.concat(deviceDisable),
      });
  },

  setDevice(device, type) {
      device.Nickname = device.Nickname.replace(type, '');
      if (device.DeviceStatus === 'ONLINE' || device.DeviceStatus === 'OFFLINE') {
          device.Image = `/images/icon${type}_on.png`;
      } else  {
          device.Image = `/images/icon${type}_off.png`;
      }
  },

  goToConfig() {
      console.log('goToConfig');
      wx.navigateTo({
          url: '/pages/config/config',

      });
  }
});
